<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/component/commonTagLib.jsp"%>
<!DOCTYPE html>
<html>
<head>
<title><spring:message code="message.button.ZhiDingJiHua"/></title>
<%@ include file="/WEB-INF/component/commonCSS.jsp"%>
<!--[if lt IE 9]>
        <script src="lib/js/html5shiv.min.js"></script>
        <script src="lib/js/respond.min.js"></script>
    <![endif]-->
</head>
<body class="skin-blue sidebar-mini fixed skin-blue-light-frame">
	
	<section class="content-header">
		<h1><spring:message code="message.list.ZhiDingCaiGouJiHua"/></h1>
		<ol class="breadcrumb">
			<li><a href="${ctx}/home.jsp"><i class="fa fa-home"></i><spring:message code="message.lable.first"/></a></li>
			<li><a href="#">交易</a></li>
			<li><a href="#"><spring:message code="message.list.DingDanGuanLi"/></a></li>
			<li class="active"><a href="#"><spring:message code="message.list.ZhiDingCaiGouJiHua"/></a></li>
		</ol>
	</section>

	<section class="content">
		<div class=" box box-success">
			<div class="box-body">
				<div class="input">
				    <div class="textalign1"><spring:message code="message.listForm.tongYongMing"/><spring:message code="message.yanZheng.maoHao"/></div>
                    <div class="boxinput">
                        <input type="text" class="textinput" id="drugName" name="drugName" placeholder="<spring:message code="message.listForm.tongYongMing"/>/<spring:message code="message.listForm.pinYinMa"/>" />
                    </div>
				    <%--<div class="textalign"><spring:message code="message.listForm.yaoPinBianMa"/><spring:message code="message.yanZheng.maoHao"/></div>
                    <div class="boxinput">
                        <input type="text" class="textinput" id="drugCode" name="drugCode"  placeholder="<spring:message code="message.listForm.yaoPinBianMa"/>" />
                    </div>--%>
					<div class="textalign1"><spring:message code="message.listForm.shengChanQiYe"/><spring:message code="message.yanZheng.maoHao"/></div>
                    <div class="boxinput">
                        <input type="text" class="textinput" id="compName" name="compName" placeholder="<spring:message code="message.listForm.shengChanQiYe"/>/<spring:message code="message.listForm.pinYinMa"/>" />
                    </div>
                    <div class="textalign1"><spring:message code="message.listForm.PeiSongQiYe"/><spring:message code="message.yanZheng.maoHao"/></div>
                    <div class="boxinput">
                        <input type="text" class="textinput" id="delComp" name="delComp" placeholder="<spring:message code="message.listForm.PeiSongQiYe"/>/<spring:message code="message.listForm.pinYinMa"/>" />
                    </div>
                 </div>
				 <div class="input">
					
                   <div class="textalign1">药品来源<spring:message code="message.yanZheng.maoHao"/></div>
                        <div class="boxinput">
                        <select class="textinput"  id="goodsSource" name="goodsSource">
                        <option value=""><spring:message code="message.select.option"/></option>
                        <c:forEach items="${list }" var="list">
                            <option value="${list.dicCode }">${list.dicName }</option>
                        </c:forEach>
                        </select>
                    </div>
                    <div class="textalign1" >
						<button type="button" onclick="gridSerch();" class="btn btn-success btn-sm" id="selectDicCompType"><spring:message code="message.button.seachSpacing"/></button>
					</div>
				</div>
                 <div class="input" style="margin-top:3px;display:none;">
					<div class="textalign1" >
						<button type="button" onclick="gridSerch();" class="btn btn-success btn-sm" id="selectDicCompType"><spring:message code="message.button.seachSpacing"/></button>
					</div>
				</div>
			</div>
		</div>
		<div id="gouwuche" style="cursor:pointer;position:absolute;z-index:9999;right:20px;padding-top:6px;">
		   <button type="button" class="btn btn-default btn-sm">
		      <i class="fa fa-shopping-cart" style="color:#dd4b39"></i>&nbsp;购物车&nbsp;<span id="theCount" style="color:#dd4b39;font-weight:600">${count }</span>
		   </button>       		       
		</div>
		<table class="jqgrid" id="gridlist"></table>
		<div id="gridpage"></div>
		<div class=" box box-warning bottongroup">
			<button type="button" onclick="batchJoin()"	id="start" class="btn btn-primary btn-sm"><spring:message code="message.button.JiaRuGouWuChe"/></button>
			<button type="button" onclick="window.location.href='toSeePurPlan.html'" class="btn btn-primary btn-sm"><spring:message code="message.list.ChaKanWoDeGouWuChe"/></button>
		</div>
	
	</section>
	<%@ include file="/WEB-INF/component/commonJS.jsp"%>
	<script type="text/javascript">
	//绑定回车
    $('#projId,#drugCode,#drugName,#compName,#delComp,#isLongTimePlan').bind('keydown', function(event) {
        if (event.keyCode == "13") {
            $('#selectDicCompType').click();
        }
    });	
    $("#drugName").focus();//加载页面光标在第一个查询框内
	
	
	
	
        $(function () {
        	$("#gouwuche").click(function(){
        		window.location = "${pageContext.request.contextPath}/purPurPlanController/toSeePurPlan.html";
        	});
        	$("#projId").HNSelect({
                url: "${pageContext.request.contextPath}/selectController/getMyProj.html", data: { ID: '' },  defaultText: "<option value=''><spring:message code="message.select.option"/></option>",
            });
        	        	
            $("#gridlist").jqGrid({
                url: "${ctx}/purPurPlanController/getPurHospCatalogList.html",
                mtype: "POST",
                datatype: "json",
                autowidth: true,
                shrinkToFit: false,
              //  height: 230,
                colNames: ['delCompCode',
                           'prodCompcode',
                           'hospProCatId',
                           '<spring:message code="message.listForm.CaiGouXianJia"/>',
                           '<spring:message code="message.listForm.CaiGouJia"/>',
                           '<spring:message code="message.listForm.CaiGouShuLiang"/>',
                           '<spring:message code="message.listForm.PeiSongQiYe"/>',
                           '<spring:message code="message.listForm.yaoPinBianMa"/>',
                           '<spring:message code="message.listForm.tongYongMing"/>', 
                           '<spring:message code="message.listForm.jiXing"/>',
                           '<spring:message code="message.listForm.guiGe"/>',
                           '<spring:message code="message.listForm.zhuanHuanXiShu"/>',
                           '<spring:message code="message.listForm.baoZhuangDanWei"/>', 
                           '<spring:message code="message.listForm.caiZhi"/>',
                           '<spring:message code="message.listForm.shengChanQiYe"/>',
                           '国药准字',
                           '<spring:message code="message.listForm.shangBianMa"/>',             
						   '药品来源',
                           '<spring:message code="message.listForm.XiangMuMingCheng"/>',
                           '',''
                           ],
                colModel: [
					{ name: 'delCompCode', index: 'ship',hidden:true, width: 50,align:"right",sortable:false}, 
					{ name: 'prodCompcode', index: 'ship',hidden:true, width: 50,align:"right",sortable:false},
					{ name: 'hospProCatId', index: 'ship',hidden:true, width: 50,align:"right",sortable:false},
					{ name: 'proPriceLimit', index: 'ship',hidden:true, width: 80,align:"right",sortable:false},
					{ name: 'proPrice', index: 'ship', width: 80,align:"right", sortable:false},
					{ name: 'buyNum', index: 'ship', width: 80, align: "right", sortable:false },
					{ name: 'delcompname', index: 'ship', width: 160, align: "left", sortable:false},
					{ name: 'drugCode', index: 'sdate',hidden:true, width: 150, align: "center",sortable:false ,
			     			formatter:function(val, opts, row) {
			     			      return "<a href=\"javascript:showDrugInfo('"+val+"');\">"+val+"</a>"
			     			} },
                    { name: 'qualityspecName', index: 'name', width: 150, align: "left" ,sortable:false },
                    { name: 'formName', index: 'stock', width: 80 , align: "center",sortable:false},
                    { name: 'specName', index: 'ship', width: 116, align: "center" ,sortable:false},
                    { name: 'factor', index: 'ship', width: 80, align: "center",sortable:false },
                    { name: 'unitName', index: 'ship', width: 80, align: "center",sortable:false },
                    { name: 'materialName', index: 'ship',hidden:true, width: 80, align: "center" ,sortable:false},
                    { name: 'prodCompname', index: 'ship', width: 120, align: "left",sortable:false/* ,
                    	formatter:function(val, opts,row) {
                   	     return "<a href='javascript:showCompInfo(\"" + row.prodCompId + "\", \"" + val + "\");'>"+val+"</a>"
                   	} */},
                   	{ name: 'drugMedicine', index: 'drugMedicine',width: 100, align: "center", sortable:false},
                    { name: 'goodsCode', index: 'sdate', width: 100, align: "center", sortable:false,hidden:true},
					{ name: 'goodsSource', index: 'ship', width: 80,align:"center",sortable:false},
                    { name: 'projName', index: 'sdate', width: 160, align: "center",sortable:false},
                    { name: 'lsBackDrugFlag', index: 'ship',hidden:true, width: 80,align:"center",sortable:false},
                    { name: 'bakPurCount', index: 'ship',hidden:true, width: 80,align:"center",sortable:false}
                    
                ],
                rowNum: 20,
                rowList: [10, 20, 50, 100],
                rownumbers: true,
                pager: "#gridpage",
                multiselect: true, 
                viewrecords: true,
                gridComplete: function(){
                	//序号列宽度自适应（参数为jqgridID）
                    autoRNWidth("gridlist");
                	//数据只有一条时默认选中（参数为jqgridID）
                    selectByOneData("gridlist");
            		var ids = jQuery("#gridlist").jqGrid('getDataIDs');
            		if ('${isSetPlanPrice}' == 'true'){
	            		for(var i=0;i < ids.length;i++){
	            			var id = ids[i];
	            			var rowdata = $("#gridlist").jqGrid('getRowData', id);
	            			c = "";
	            			if (rowdata.lsBackDrugFlag == 'true'){
	            				jQuery("#gridlist").jqGrid('setRowData',ids[i],{proPrice:rowdata.proPrice});
	            			} else {
	            				if (rowdata.proPrice.length > 0){
		            				c = "<input class=\"\" style=\"width:80px;text-align: right;\" type=\"text\" id=\"proPrice"+id+"\" value=\""+rowdata.proPrice+"\" />";
		            			} else {
		            				c = "<input class=\"\" style=\"width:80px;text-align: right;\" type=\"text\" id=\"proPrice"+id+"\" value=\""+rowdata.proPriceLimit+"\" />";
		            			}
	            				jQuery("#gridlist").jqGrid('setRowData',ids[i],{proPrice:c});
	            			}
						}
                    } else {
                    	for(var i=0;i < ids.length;i++){
	            			var id = ids[i];
	            			var rowdata = $("#gridlist").jqGrid('getRowData', id);
	            			if (rowdata.proPrice.length > 0){
	            				jQuery("#gridlist").jqGrid('setRowData',ids[i],{proPrice:rowdata.proPrice});
	            			} else {
	            				jQuery("#gridlist").jqGrid('setRowData',ids[i],{proPrice:rowdata.proPriceLimit});
	            			}
			            			
						}
                    }
            		for(var i=0;i < ids.length;i++){
            			var id = ids[i];
            			var rowdata = $("#gridlist").jqGrid('getRowData', id);
            			if (rowdata.lsBackDrugFlag == 'false'){
            				a = "<input class=\"buyNum\" style=\"width:75px;text-align: right;\" type=\"text\" id=\"buyNum"+id+"\" value=\""+rowdata.buyNum+"\" />";
                			jQuery("#gridlist").jqGrid('setRowData',ids[i],{buyNum:a});
            			} else {
            				if ("${bakBatchBuy}" == "true"){
            					a = "<input class=\"buyNum\" style=\"width:75px;text-align: right;\" type=\"text\" id=\"buyNum"+id+"\" value=\""+rowdata.buyNum+"\" />";
                    			jQuery("#gridlist").jqGrid('setRowData',ids[i],{buyNum:a});
            				}
            				jQuery("#gridlist").jqGrid('setRowData',ids[i],{buyNum:rowdata.bakPurCount});
            			}
            		}
            		
                      $('.buyNum').keyup(function(event){
                    	  var num =$(this).attr("id").replace(/([^0-9])/ig,'');
                    	  changeSelectChecked("#gridlist",num,$(this).val(),event);
                      }); 
                      
                       
                      
                      
            	},
                caption: "<spring:message code="message.list.proddruginfoStdlist"/>",
                jsonReader: {
                    repeatitems: false,
                    id: "ids"
                }
            });
            $("#gridlist").jqGrid('navGrid', '#gridpage', { add: false, edit: false, del: false, search: false, refresh: false });
            $("#gridlist").jqGrid("setGridHeight", create());
            $(window).trigger("resize");
        });
        
        
        //查询
        function gridSerch() {
        	var projId = $.trim($("#projId").val());
        	var drugCode = $.trim($("#drugCode").val());
        	var drugName = $.trim($("#drugName").val());
        	var compName = $.trim($("#compName").val());
        	var delComp = $.trim($("#delComp").val());
        	var goodsSource = $.trim($("#goodsSource").val());
    		$("#gridlist").jqGrid('setGridParam',{  
    	        datatype:'json',
    	        postData:{
    	        		"projId":projId,
    	        		"drugCode":drugCode,
    	        		"drugName":drugName,
    	        		"compName":compName,
    	        		"delComp":delComp,
    	        		"goodsSource":goodsSource},
    	        		page:1
    	    }).trigger("reloadGrid"); //重新载入  
    	}
        
        
        
        
        //加入计划
        function batchJoin(){
        	var reg = new RegExp(regexEnum.intege1);
        	var rowIds = $("#gridlist").jqGrid("getGridParam", "selarrrow") || [];
    		if (rowIds.length <= 0) {
    			$.HN.message.alert("请选择要操作的药品！", "<spring:message code="message.HN.alert.type"/>", "warn");
    			return;
    		}
          	
    		var list = [];
    		var infos = "";
            for (var i=0; i<rowIds.length; i++) {
                rowId = rowIds[i];
                var rowdata = $("#gridlist").jqGrid('getRowData', rowId);
                var buynum = "";
    			if (rowdata.lsBackDrugFlag == "true" && "${bakBatchBuy}" != "true"){
    				buynum = rowdata.buyNum;
    			} else {
    				buynum = $.trim($("#buyNum"+rowId).val());
    			}
                if ($.trim(buynum).length < 1){
                    $.HN.message.alert("<spring:message code="message.yanZheng.pleaseTianXieCaiGouShuLiang"/>！", "<spring:message code="message.HN.alert.type"/>", "warn");
                    return;
                }
                if($.trim(buynum).length > 10){
                    $.HN.message.alert('<spring:message code="message.yanZheng.CaiGouShuLiangTaiChang"/>', '<spring:message code="message.HN.alert.type"/>', 'warn');
                     return;
                } 
                var proprice = "";
                if ("${isSetPlanPrice}" == "true"){
                	if (rowdata.lsBackDrugFlag == "true"){
        				proprice = rowdata.proPrice;
        			} else {
        				proprice = $.trim($("#proPrice"+rowId).val());
        			}
                } else {
                	proprice = rowdata.proPrice;
                }
                if ($.trim(proprice).length < 1){
                    $.HN.message.alert("采购价不能为空！", "<spring:message code="message.HN.alert.type"/>", "warn");
                    return;
                }
                if(!reg.test($.trim(buynum))){
                    $.HN.message.alert("<spring:message code="message.yanZheng.CaiGouShuLiangWeiZhengZhengShu"/><spring:message code="message.yanZheng.ganTanHao"/><spring:message code="message.yanZheng.qingChongXinShuRu"/>", "<spring:message code="message.HN.alert.type"/>", "warn");
                    return;
                }else{
                    list.push({"hospProCatId":rowdata.hospProCatId,"buyNum":$.trim(buynum),"proPrice":proprice,"drugName":rowdata.qualityspecName,"proLimitPrice":rowdata.proPriceLimit,"lsBackDrugFlag":rowdata.lsBackDrugFlag}); 
                    if(3>i){
                    	infos+=rowdata.qualityspecName+"<br/>"
                    	if(2==i){
                    		infos+="...";
                    	}
                    }
                }
            }
			//获取选中的ID及对应的名字（参数:jqgridId,名字列名，状态列名，当前操作即列表中显示的值），返回infos[0]是ID串，infos[1]是名字串
        	// var infos = batchSwitchStatus("gridlist","qualityspecName","qualityspecName","<spring:message code='message.listForm.tongYongMing'/>","<spring:message code='message.listForm.tongYongMing'/>");
    		/* $.HN.message.confirm('确定加入所选的药品吗？<br/>'+infos[1].replace(/\,/g,"<br/>"), '', '').on(function (e) {  */
    			 $.HN.message.confirm("确定加入以下药品吗？<br/>"+infos+"", '', '').on(function (e) {
    			if (e) {
    				
    				
    	    		$.post("${pageContext.request.contextPath}/purPurPlanController/batchJoinPlan.html", {
    	    			"array" : json.jsonToString(list)
    	    		}, function(result) {
    	    			if (result.success) {
    	    				toFly(result.conditions.count);
    	    				gridSerch();
    	    				$.HN.message.alert("加入成功！", "<spring:message code="message.HN.alert.type"/>", "success");
    	    			} else {
    	    				$.HN.message.alert(result.msg, "<spring:message code="message.HN.alert.type"/>", "error");
    	    			}
    	    			
    	    		}, "json");
    			}
    		});
        }
        
        
        function toFly(count){ 
        	$("body").append("<div id='flyCar' style='width:40px;height:40px;background:url(${ctx}/lib/img/gift.png) no-repeat ;position:absolute;z-index:1;left:"+$('#start').offset().left+"px;top:"+$('#start').offset().top+"px;' ></div>");
        	var startX = $("#start").offset().left;
        	var startY = $("#start").offset().top;
        	var endX = $("#gouwuche").offset().left;
        	var endY = $("#gouwuche").offset().top;
        	var dx = endX - startX;
        	var dy = endY - startY;
        	getFly(dx,dy,endX,endY,count);
        }
        
        function getFly(dx,dy,endX,endY,count){
        	$("#flyCar").css("left",$("#flyCar").offset().left+dx/40);
        	$("#flyCar").css("top",$("#flyCar").offset().top+dy/40);
        	while($("#flyCar").offset().left > endX && $("#flyCar").offset().top < endY){
        		$("#theCount").html(count);
        		$("#flyCar").remove();
        		return;
        	}
			setTimeout(function(){
				getFly(dx,dy,endX,endY,count);
			},20);
        	  
        }
        
    </script>
</body>
</html>